﻿using System;
using System.Data;
using System.Web.UI;
using System.Text;
using System.IO;
using QzEbook.Admin;
using QzEbook.AccessObject;
using QzEbook.Common;
using System.Collections.Specialized;

namespace qzEbook.module
{

    public partial class bookList : System.Web.UI.UserControl
    {
        private StringBuilder sb;
        private int pageSize = 9;
        private int countPage = 1;
        private int currentIndex = 1;


        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
            PaginationHTML();

        }
        private void BindData()
        {
            SessionUser userSession = SessionManager.LoginUser;

            Book book = new Book();
            DataTable table = book.selectUser(userSession.Uid);

            DataRowCollection rows = table.Rows;
            int length = rows.Count;
            countPage = length % pageSize == 0 ? (int)(length / pageSize) : (int)(length / pageSize) + 1;
            currentIndex = Request["Pi"] == null ? 1 : int.Parse(Request["Pi"]);

            sb = new StringBuilder("\r\n");


            if (table.Rows.Count == 0)
            {
                sb.Append("<div style='padding:10px 15px; line-height:20px;color:#990000;'><h4><strong>提示:</strong></h4>你暂无电子书,可通过用户控制点击.<strong>新增电子书</strong></div>");
                return;
            }
            sb.Append("<h3>我的电子书:</h3>");
            sb.AppendLine("<ul class='booklist' id='booklist'>");

            int start = (currentIndex-1) * pageSize;
            int end = start + pageSize;
            end = end >= length ? length : end;
            
            for (int i = start; i <end ; i++)
            {
                DataRow row = rows[i];
                book = book.bindThis(row);

                sb.AppendLine("<li>");
                sb.AppendFormat("<a href='PageList.xhtml?Bid={0}'>", book.BookId);

                string thumb = string.Format("../{0}", book.ThumbFullPath);
                if (!File.Exists(Server.MapPath(thumb)))
                {
                    thumb = "../images/siteIcon/error.gif";
                }
                double bookByte;
                try
                {
                    bookByte = Math.Round(book.Costbyte / 1024, 2);
                }
                catch
                {
                    bookByte = 0;
                }
                sb.AppendFormat("<img src='{0}?rnd={4}' alt='查看{1}{2}占用:{3}M' />{2}", thumb, book.Title, Environment.NewLine, bookByte, DateTime.Now.Second);
                sb.AppendLine("</a>");

                sb.AppendFormat("<ins>{0} /{1}M", book.Title, bookByte);

                sb.AppendLine("<cite>");

                int Bid = (int)row["BID"];
                sb.AppendFormat("<a href='DefaultSet.xhtml?Bid={0}'><img src='../images/edit-comment-orange.gif' alt='编辑基本信息' /></a>", Bid);
                sb.AppendFormat("<a href='ShowView.xhtml?Bid={0}'><img src='../images/icon_swf.gif' alt='预览效果' /></a>", Bid);
                sb.AppendFormat("<a href='Delete.xhtml?Bid={0}'><img src='../images/delete-folder-orange.gif' alt='删除此书' /></a>", Bid);
                if (book.isSale)
                {
                    sb.Append("<dfn><img src='../images/siteIcon/buy.gif' alt='收费' /></dfn>");
                }
                if (book.isSelf)
                {
                    sb.Append("<dfn><img src='../images/siteIcon/key.gif' alt='私有' /></dfn>");
                }
                sb.Append(book.getAuditingHTML("../"));

                sb.AppendLine("</cite>");
                sb.AppendLine("</ins>");
                sb.AppendLine("</li>");
            }
            sb.AppendLine("</ul>");
        }

        private void PaginationHTML()
        {
            if (this.countPage <= 1) return;
            
            NameValueCollection list = Request.QueryString;
            string url = "?";
            /*string namevalue = string.Empty;
            foreach (string item in list.AllKeys)
            {
                namevalue = list[item];
                if (item.ToLower() != "pi")
                {
                    url = string.Format("{0}{1}={2}&", url, item, namevalue);
                }
            }*/
            
            sb.Append("<ul class=\"pagination\">");

            if (currentIndex > 1)
            {
                sb.AppendFormat("<li><a href=\"{0}Pi={1}\" titile=\"上一页\">«  </a></li>", url, currentIndex - 1);
            }
            for (int i = 1; i <= countPage; i++)
            {
                if (i == currentIndex)
                {
                    sb.AppendFormat("<li class=\"current\">{0}</li> ", i);
                }
                else
                {
                    sb.AppendFormat("<li><a href=\"{0}Pi={1}\" title=第{0}页>{1}</a></li> ", url, i);
                }
            }
            if (currentIndex < countPage)
            {
                sb.AppendFormat("<li><a href=\"{0}Pi={1}\" title=\"下一页\">  »</a></li>", url, currentIndex + 1);
            }
            sb.AppendFormat("<li>{0}/{1}</li> ", currentIndex, countPage);
            sb.AppendLine("</ul>");
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(sb.ToString());
            base.Render(writer);
        }
    }
}